package com.yw.filter;

import com.yw.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created By 张永伟 on 2018/6/5.
 */
public class MyInterceptor implements HandlerInterceptor {

    /**
     * 完成整个请求之后调用
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception e)throws Exception {
        System.out.println("3333333333333333333");
    }

    /**
     * 进入controller方法之后，渲染视图之前调用
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView model) throws Exception {
        System.out.println("2222222222222222222");
    }

    /**
     * 进入controller方法之前调用
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object object) throws Exception {
        System.out.println("1111111111111111111");
        boolean flag;
        //如果获取不到用户，跳转到登陆页面
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            response.sendRedirect("/");
//            response.sendRedirect("/user/login");
            flag = false;
        } else {
            flag = true;
        }
        return flag;
    }
}